package org.spring.springboot.app.adv.advcombinfo.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.adv.advcombhard.dao.AdvCombHardDao;
import org.spring.springboot.app.adv.advcombhard.domain.po.AdvCombHardPO;
import org.spring.springboot.app.adv.advcombinfo.dao.AdvCombInfoDao;
import org.spring.springboot.app.adv.advcombinfo.domain.po.AdvCombInfoPO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoSaveInVO;
import org.spring.springboot.app.adv.advcombinfo.domain.vo.AdvCombInfoSearchVO;
import org.spring.springboot.app.adv.advcombinfo.service.IAdvCombInfoService;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.sys.systemoption.service.ISystemOptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class AdvCombInfoServiceImpl implements IAdvCombInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdvCombInfoServiceImpl.class);

    @Autowired
    private AdvCombInfoDao advCombInfoDao;

    @Autowired
    private AdvCombHardDao advCombHardDao;
    @Autowired
    private ISystemOptionService systemOptionService;
    @Override
    public AdvCombInfoPO find(String id) {
        return advCombInfoDao.selectByPrimaryKey(id);
    }

    @Override
    public List<AdvCombInfoPO> findPage(AdvCombInfoSearchVO searchVO, int pageNum, int pageSize) {
        return advCombInfoDao.findPage(searchVO, pageNum,pageSize);
    }

    @Override
    public List<AdvCombInfoPO> findAll(AdvCombInfoSearchVO searchVO) {
        return advCombInfoDao.findAll(searchVO);
    }

    @Override
    public R save(AdvCombInfoSaveInVO vo) {
        R r = new R();
        if(vo.getTurnPrice()==null){
            return r.fail().setMessage("轮播广告套餐价格不能为空");
        }
        if(vo.getAccuratePrice()==null){
            return r.fail().setMessage("精准广告套餐价格不能为空");
        }
        if(vo.getTurnPrice().compareTo(new BigDecimal(systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_TURN_PRICE)))==-1){
            return r.fail().setMessage("轮播广告套餐价格不能低于："+systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_TURN_PRICE));
        }
        if(vo.getAccuratePrice().compareTo(new BigDecimal(systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_ACCURATE_PRICE)))==-1){
            return r.fail().setMessage("轮播广告套餐价格不能低于："+systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_ACCURATE_PRICE));
        }
        AdvCombInfoPO po = new AdvCombInfoPO();
        BeanUtils.copyProperties(vo,po);
        po.preInsert();
        int code = advCombInfoDao.insertSelective(po);
        if (code == 0) {
            return r.fail().setMessage("保存失败");
        }
        //保存设备
        saveCombHards(po.getId(),vo.getHardIds());
        return r.success();
    }

    @Override
    public R update(AdvCombInfoSaveInVO vo) {
        R r = new R();
        if(vo.getTurnPrice()!=null&&vo.getTurnPrice().compareTo(new BigDecimal(systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_TURN_PRICE)))==-1){
            return r.fail().setMessage("轮播广告套餐价格不能低于："+systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_TURN_PRICE));
        }
        if(vo.getAccuratePrice()!=null&&vo.getAccuratePrice().compareTo(new BigDecimal(systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_ACCURATE_PRICE)))==-1){
            return r.fail().setMessage("轮播广告套餐价格不能低于："+systemOptionService.getString(Constants.ADV_COMB_INFO_MIN_ADV_ACCURATE_PRICE));
        }
        AdvCombInfoPO po = new AdvCombInfoPO();
        BeanUtils.copyProperties(vo,po);
        po.preUpdate();
        int code= advCombInfoDao.updateByPrimaryKeySelective(po);
        if (code == 0) {
            return r.fail().setMessage("保存失败");
        }
        if (StringUtils.isNotBlank(vo.getHardIds())) {
            //删除所有设备
            advCombHardDao.deleteByCombId(po.getId());
            //保存设备
            saveCombHards(po.getId(),vo.getHardIds());
        }
        return r.success();
    }

    private void saveCombHards(String combId,String hardIds) {
        String[] arr = hardIds.split(",");
        for (String hard : arr){
            AdvCombHardPO hardPO = new AdvCombHardPO();
            hardPO.setAdvCombId(combId);
            hardPO.setHardId(hard);
            advCombHardDao.insertSelective(hardPO);
        }
    }

    @Override
    public int delete(AdvCombInfoPO po) {
        po.preDelete();
        return advCombInfoDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public List<AdvCombHardPO> queryHardList(String combId) {
        return advCombHardDao.queryListByCombId(combId);
    }
    @Override
    public int queryHardCount(String combId) {
        return advCombHardDao.queryCountByCombId(combId);
    }

    @Override
    public List<AdvCombHardPO> queryInCombHardList(String hardIds) {
        return advCombHardDao.queryInCombHardList(hardIds);
    }
}
